package httpApi

import (
	"net/http"
	"strings"
	"strconv"
	"fmt"
	"io/ioutil"
	"time"
	"monitor-control/config"
	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func CallBackApi(id int64,ip string,servicePath string ) (err error) {
	defer func(){
		if e := recover(); e != nil{
			err = e.(error)
		}
	}()
	dataConfig := config.Init()

	resJson := "{\"id\": "+strconv.FormatInt(id, 10)+", 	\"cameraIp\": \""+ip+"\", 	\"code\": 1, 	\"path\": \""+servicePath+"\" }"
	req, _ := http.NewRequest("POST", dataConfig.IotGetServer, strings.NewReader(resJson))
	req.Header.Set("Content-Type", "application/json")
	clientHttp := &http.Client{}
	res, err := clientHttp.Do(req)
	if err != nil {
    	fmt.Println(err)
	}
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)
	fmt.Println("回调内容:"+resJson)
	fmt.Println(string(body))
	return err
}

func UpdataOSS(fileName string,name string) (bool,string) {
	year := time.Now().Year()
	month := time.Now().Month() //time.Now().Month().String()
	day := time.Now().Day()

	servicePath := "gate_file/gate_video/" + strconv.Itoa(year) + "/" + strconv.Itoa(int(month)) + "/" + strconv.Itoa(day) + "/" + name
	//fmt.Println(servicePath)

	client, err := oss.New("http://oss-cn-shenzhen.aliyuncs.com", "LTAIyiecz6Hrw0hK", "OjLiTyPBkDGE103p0Wg1YVzh9lUcr0")
	if err != nil {
		fmt.Println("new err")
		return false,""
	}
	bucket, err := client.Bucket("usedcar-prod")
	if err != nil {
		// HandleError(err)
		fmt.Println("Bucket err")
		return false,""
	}
	err = bucket.PutObjectFromFile(servicePath, fileName)
	if err != nil {
		// HandleError(err)
		fmt.Println(err.Error())
		fmt.Println("PutObjectFromFile err")
		return false,""
	}

	return true,servicePath

}